module executor.group.client.GroupExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import client.ImClient;
import std.array;
import std.conv : to;

@TransportClient(msgClientName)
class GroupExecutor : AbstractExecutor!(GroupExecutor){

  @MessageId(MESSAGE.CMD_JOIN_GROUP)
  void joinGroup(TransportContext ctx, MessageBuffer buffer)
  {
    auto joinGroup = new JoinGroup();
    buffer.data.fromProtobuf!JoinGroup(joinGroup);
    TransportSession[] clients =  ImClient.getInstance().getImClient(to!ulong(joinGroup.destination));
    foreach (TransportSession client ; clients)
    {
        client.send(new MessageBuffer(MESSAGE.CMD_JOIN_GROUP,joinGroup.toProtobuf.array));
    }
    //if (c !is null)
    //{
    //  c.send(new MessageBuffer(MESSAGE.CMD_JOIN_GROUP,joinGroup.toProtobuf.array));
    //}else
    //{
    //  logWarning("joinGroup user %d is offline",joinGroup.destination);
    //}
  }

  @MessageId(MESSAGE.CMD_INVITE_GROUP)
  void inviteGroup(TransportContext ctx, MessageBuffer buffer)
  {
    //auto inviteGroup = new InviteGroup();
    //buffer.data.fromProtobuf!InviteGroup(inviteGroup);
    //TransportSession[] clients =  ImClient.getInstance().getImClient(to!ulong(inviteGroup.destination));
    //foreach (TransportSession client ; clients)
    //{
    //  client.send(new MessageBuffer(MESSAGE.CMD_INVITE_GROUP,inviteGroup.toProtobuf.array));
    //}
    //if (c !is null)
    //{
    //  c.send(new MessageBuffer(MESSAGE.CMD_INVITE_GROUP,inviteGroup.toProtobuf.array));
    //}else
    //{
    //  logWarning("inviteGroup user %d is offline",inviteGroup.destination);
    //}
  }

  @MessageId(MESSAGE.CMD_GROUP_MEMBER_CHANGE)
  void groupMemberChange(TransportContext ctx, MessageBuffer buffer)
  {

    auto chatMapAction = new ChatMapAction();
    buffer.data.fromProtobuf!ChatMapAction(chatMapAction);
    logInfo("CMD_GROUP_MEMBER_CHANGE %s ----- $s" , chatMapAction.chatId , chatMapAction.destination);
    TransportSession[] clients =  ImClient.getInstance().getImClient(to!ulong(chatMapAction.destination));
    foreach(TransportSession client ; clients)
    {
      logInfo("send to : %s",chatMapAction.destination);
      client.send(new MessageBuffer(MESSAGE.CMD_GROUP_MEMBER_CHANGE,chatMapAction.toProtobuf.array));
    }
    //if (c !is null)
    //{
    //  c.send(new MessageBuffer(MESSAGE.CMD_GROUP_MEMBER_CHANGE,chatMapAction.toProtobuf.array));
    //}else
    //{
    //  logWarning("groupMemberChange user %d is offline",chatMapAction.destination);
    //}
  }

  @MessageId(MESSAGE.CMD_APPLY_GROUP)
  void applyGroup(TransportContext ctx, MessageBuffer buffer)
  {
    auto applyGroup = new ApplyGroup();
    buffer.data.fromProtobuf!ApplyGroup(applyGroup);
    TransportSession[] clients =  ImClient.getInstance().getImClient(to!ulong(applyGroup.destination));
    foreach (TransportSession client ; clients)
    {
      client.send(new MessageBuffer(MESSAGE.CMD_APPLY_GROUP,applyGroup.toProtobuf.array));
    }
  }

  @MessageId(MESSAGE.CMD_SET_GROUP_NAME_RES)
  void setName(TransportContext ctx, MessageBuffer buffer)
  {
    auto setGroupName = new SetGroupNameRES();
    buffer.data.fromProtobuf!SetGroupNameRES(setGroupName);
    TransportSession[] clients =  ImClient.getInstance().getImClient(to!ulong(setGroupName.userId));
    foreach (TransportSession client ; clients)
    {
      client.send(new MessageBuffer(MESSAGE.CMD_SET_GROUP_NAME_RES,setGroupName.toProtobuf.array));
    }
  }

}

